package com.radiation.rms.common;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.radiation.rms.entity.AclUserDepartment;
import com.radiation.rms.entity.Device;
import com.radiation.rms.entity.User;
import com.radiation.rms.service.AclUserDepartmentService;
import com.radiation.rms.service.AreaService;
import com.radiation.rms.service.DeviceService;
import com.radiation.rms.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 10626
 */
@Component
@Slf4j
public class RMSCommon {
//    @Autowired
//    private AreaService areaService;

    @Autowired
    private UserService userService;

    @Autowired
    private AclUserDepartmentService aclUserDepartmentService;

    @Autowired
    private DeviceService deviceService;

    public List<String> getDepartmentIds(String username){
        log.error("当前登录名：{}", username);
        List<String> collect = null;
        if (username.equals(RMSConstant.ADMIN)) {
            List<Device> list = deviceService.list(null);
            collect = list.stream().map(Device::getDepartmentId).collect(Collectors.toList());
        } else {
            User user = userService.selectByUsername(username);
            List<AclUserDepartment> departments = aclUserDepartmentService.list(new QueryWrapper<AclUserDepartment>().eq("user_id", user.getId()));
            collect = departments.stream().map(AclUserDepartment::getDepartmentId).collect(Collectors.toList());
        }
        return collect;
    }
    public List<String> getUserIds(String username){
        log.error("当前登录名：{}", username);
        List<String> collect = null;
        if (username.equals(RMSConstant.ADMIN)) {
            List<User> list = userService.list(null);
            collect = list.stream().map(User::getId).collect(Collectors.toList());
        } else {
            User user = userService.selectByUsername(username);
            collect = aclUserDepartmentService.getUserIds(user.getId());
        }
        return collect;
    }
    public List<String> getUserByUserId(String userId){
        List<AclUserDepartment> departments = aclUserDepartmentService.list(new QueryWrapper<AclUserDepartment>().eq("user_id",userId));
        return departments.stream().map(AclUserDepartment::getDepartmentId).collect(Collectors.toList());
    }

}
